<?php
/***
 * 异步说明：
 * 2024-06-13：和产品雨熙一起讨论后，目前采用消费一笔订单，推送一条数据给雨熙接口，即当用户启动机器后异步推送订单的数据，订单类型除了消费积分外的所有订单都须要推送。
 * 
 * 
 */ 
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Models\XcSiteOrder;
use Deyi;


class PushSiteOrderData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Deyi;

    protected $siteorder;//数据

    //public $tries = 3; // 失败最多尝试3次
    //public $timeout = 60; //任务运行的超时时间,60秒

    public $url = 'http://wx.proxy.deyi.net/wxapp/points/transforms';//接口url

    public $key = 'Wedding!';//访问接口的key

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($siteorder)
    {
        //
        $this->siteorder = $siteorder;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //执行的脚本
        //Log::info('call one time,'.$this->siteorder);
        $siteorder = XcSiteOrder::where("id",$this->siteorder->id)->with(['user',
            'washmode' => function($q){
                        $q->with("factorywashmode");
                    }])->orderby("id","asc")->get()->toArray();
        //dd($siteorder);
        Log::info(' get siteorder data : '.json_encode($siteorder));
        if (!empty($siteorder)){
            foreach ($siteorder as $k => $v) {
                $data['type'] = 2;
                $data['order_amount'] = $v['money'];
                $data['goods_name'] = $v['washmode']['factorywashmode']['name'].$v['money'];
                $data['order_sn'] = $v['order_sn'];
                $data['mobile']    = $v['user']['mobile'];
                //print_r($data);

                $eny_data = $this->encrypt(json_encode($data), $this->key);
                Log::info(' post encrypt data : '.$eny_data);
                $datac = ['token' => $eny_data];
                //print_r($data);
                //echo $eny_data;exit;
                $result = $this->curlPost($this->url.'?token='.$eny_data,[]);
                return response()->json(json_decode($result));
                //$result  = $this->decrypt($content,$this->key);
                Log::info(' api return data : '.json_encode($result));
            }
        }

        
        
    }

    /**
     * 处理一个失败的任务
     *
     * @return void
     */
    public function failed()
    {
        // 当任务失败时会被调用...
        Log::info('当任务失败时会被调用', ['path' => __METHOD__, 'line' => __LINE__]);
    }


    /*
    //解密
    public function decrypt($data, $key)
    {
        return openssl_decrypt(base64_decode(hex2bin($data)), 'DES-ECB', $key, OPENSSL_RAW_DATA);
    }

    //加密
    public function encrypt($data, $key)
    {
        return bin2hex(base64_encode(openssl_encrypt($data, 'DES-ECB', $key, OPENSSL_RAW_DATA)));
    }
    */


    /**
     * 接口post请求
     * @param  [type] $url       请求接口url
     * @param  [type] $postdata  请求数据
     * @return [array]           返回结果数组
     */
    public function CurlPost( $url, $postdata ) {

        //$header = ['Accept: application/json'];
        $header = ['Content-Type: application/xwww-form-urlencoded'];

        //'application/xwww-form-urlencoded'
        $curl   = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
        $data   = curl_exec($curl);
        $error  = curl_error($curl);
        //dd($data,$error);

        if ($error) {
            Log::info("yuxi api error :".json_encode($error));
            return $error;
        }else{
            Log::info("yuxi api success: ".json_encode($data));
            return $data;
        }
        curl_close($curl);
    }
}
